package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campus.commons.entity.Menu;
import com.campus.app.mapper.MenuMapper;
import com.campus.app.service.MenusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
@Validated
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenusService {
    @Autowired
    private MenuMapper menuMapper;



    /**
     * 递归整理菜单；
     * @param menu
     * @param childMenu
     * @param filterId
     */
    private  void menuToSort(Menu menu, List<Menu> childMenu,int filterId){
        for (Menu m : childMenu) {
            // 如果子菜单的pid 父是这个就加进去
            if (m.getPid() .intValue()== menu.getId().intValue()) {
//                如果是指定类型则跳过 ----易错点。。。IsMenu字段为空
                if (m.getIsmenu().intValue()!=filterId) {
                    menu.getChild().add(m);
                }
                menuToSort(m,childMenu,filterId);
            }
        }
    }

    /**
     * 排序菜单；过滤掉 菜单类型是 4：功能的菜单
     * @param menus
     * @return
     */
    private List<Menu> ConverterMenus(List<Menu> menus) {
        return ConverterMenus(menus, 0);
    }

    /**
     * 排序菜单；并过滤指定菜单类型
     * @param menus 要排序的菜单列表；
     * @param filterId 过滤的类型根据 IsMenu字段；
     * @return
     */
    private List<Menu> ConverterMenus(List<Menu> menus,int filterId) {
        if (menus.size()==0||menus==null){return null;}
        Menu menu = new Menu(-1);
        menuToSort(menu, menus, filterId);
        return menu.getChild();
    }


    @Override
    public Result<List<Menu>> getMenusByRole(Integer roleId) {
        if (roleId==1) {return getAllMenusFilter();}
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enabled", 1);
        queryWrapper.inSql("id", "in (select * from role_menu rm where rm.roleId="+roleId+")");
        return Result.result(ConverterMenus(menuMapper.selectList(queryWrapper)));
    }



    @Override
    public Result addMenu(Menu menu) {
        return Result.result(menuMapper.insert(menu));
    }

    @Override
    public Result delMenu(Integer id) {
        return Result.result(menuMapper.deleteById(id));
    }



    @Override
    public Result upMenu(Menu menu) {
        return Result.result(menuMapper.updateById(menu));
    }

    //
    @Override
    public Result<List<Menu>> getAllMenus() {
        return Result.result(menuMapper.selectList(null));

    }

    @Override
    public Result<List<Menu>> getAllMenusFilter() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enabled", 1);
        return Result.result(ConverterMenus(menuMapper.selectList(queryWrapper),4));
    }
}
